<?php

namespace app\admin\model\user;

use think\Model;

class Config extends Model
{
    // 表名
    protected $name = 'user_config';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    
    // 追加属性
    protected $append = [

    ];
    public static function init()
    {
        self::event('before_insert', function ($data) {   
            if(!empty($data['dpi']) && is_json($data['dpi'])){
                $dpi = json_decode($data['dpi'],true);
                $data['dpi'] = json_encode($dpi,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);   
            }
            if(!empty($data['probe']) && is_json($data['probe'])){
                $probe = json_decode($data['probe'],true);
                $data['probe'] = json_encode($probe,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);   
            }
        });
        self::event('before_update', function ($data) {
            // $dpi = json_decode($data['dpi'],true);
            // $data['dpi'] = json_encode($dpi,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
            if(!empty($data['dpi']) && is_json($data['dpi'])){
                $dpi = json_decode($data['dpi'],true);
                $data['dpi'] = json_encode($dpi,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);   
            }
            if(!empty($data['probe']) && is_json($data['probe'])){
                $probe = json_decode($data['probe'],true);
                $data['probe'] = json_encode($probe,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);   
            }
            $time = time();
            $data['updatetime'] = $time;
        });
    }
    public function user()
    {
        return $this->belongsTo('app\admin\model\User', 'user_id', 'id');
    }

    







}
